﻿using Fantom.Drawing;

namespace Fantom.Models
{
	internal class Shape : EchoBaseObject, IShape
	{
		#region events
		public event SelectableEventHandler OnSelect;
		public event SelectableEventHandler LostSelect;
		#endregion

		#region properties

		public IApplication Application { get; set; }

		public IFantomObject Parent { get; set; }

		
		public bool IsSelected
		{
			get => _isSelected;
			set
			{
				var temp = _isSelected;

				if (Parent != null)
				{
					if (value)
					{
						(Parent as Shapes).UnSelectAll();
						(Parent as Shapes).Select(this);
						OnSelect?.Invoke(this.Parent as IShapes, new[] { this });
					}

					else
					{
						(Parent as Shapes).UnSelect(this);
						LostSelect?.Invoke(this.Parent as IShapes, new[] { this });
					}
				}

				PropertyChangedInvoke();
			}
		}
		private bool _isSelected = false;

		public int Id { get; set; }
		
		public string Name
		{
			get => _name;
			set
			{
				_name = value;
				PropertyChangedInvoke();
			}
		}
		private string _name = "unknown";

		public int ZOrderPosition => (Parent as Shapes).IndexOf(this);

		public ShapeType Type { get; set; } = ShapeType.Common;

		public ILocks Locks { get; private set; }

		public RectTransform Transform { get; private set; }

		public ITextBody TextBody { get; set; } = null;

		public string ReadOnlyText
		{
			get
			{
				if (Type == ShapeType.Common)
					return TextBody.ReadOnlyText;
				return "";
			}
		}

		public ICustomXmlList CustomXmlList { get; private set; } = new CustomXmlList();

		public Tags Tags { get; private set; } = new Tags();

		private bool _visibility = true;
		public bool Visibility
		{
			get => _visibility;
			set
			{
				if (_visibility != value)
				{
					_visibility = value;
					PropertyChangedInvoke();
				}

			}
		}

		public ILineFormat Line { get; set; } = new LineFormat();
		public IFillFormat Fill { get; set; } = new FillFormat();

		public IAdjustments Adjustments { get; set; }

		public ShapePresetType PresetType { get; set; }

		public bool IsTextBox { get; set; } = false;

		#endregion

		#region ctors
		public Shape()
		{
			Transform = new RectTransform(this);
			Locks = new Locks() { Shape = this };
		}
		#endregion

		#region methods

		public override string ToString()
		{
			return string.Format("Type: {1, -10},Name: {0,-20}", Name.Shorten(), Type.ToString());
		}

		public void Select()
		{
			if (Locks.NoSelection) return;

			IsSelected = true;

		}

		public void UnSelect()
		{
			if (Locks.NoSelection) return;

			IsSelected = false;
		}

		/// <summary>
		/// 强制且不触发事件地将指定图形拨到选中状态。
		/// </summary>
		public void SelectForced()
		{
			_isSelected = true;
			PropertyChangedInvoke("IsSelected");
		}

		/// <summary>
		/// 强制且不触发事件地将指定图形拨回未选状态。
		/// </summary>
		public void UnSelectForced()
		{
			_isSelected = false;
			PropertyChangedInvoke("IsSelected");
		}


		public void ZOrder(ZOrderCommandEnum zordercmd)
		{
			if (Locks.NoMove) return;

			int id = ZOrderPosition;
			var shps = Parent as Shapes;
			switch (zordercmd)
			{
				case ZOrderCommandEnum.BringForward:
					shps.Move(id, id + 1);
					break;
				case ZOrderCommandEnum.BringToFront:
					shps.Move(id, shps.Count - 1);
					break;
				case ZOrderCommandEnum.msoSendBackward:
					shps.Move(id, id - 1);
					break;
				case ZOrderCommandEnum.msoSendToBack:
					shps.Move(id, 0);
					break;
				default:
					break;
			}
		}

		/// <summary>
		/// 触发渲染更新事件，更新Transform。
		/// </summary>
		public void RaiseRenderUpdate()
		{
			PropertyChangedInvoke("Transform");
		}


		#endregion
	}
}
